#include "user_uart_obj.h"
#include "uart_obj.h"


/*
	串口与功能对应关系
*/

/*---------------------------------------------串口1-对象-------------------------------------------------------------------*/
#define UART1_RX_FIFO_DEEP 21
#define UART1_RX_FIFO_SINGLE_BUFF_SIZE 12

uart_obj_t uart_obj1 = {0};

uint8_t uart1_tx_buf[128] = {0};

uint8_t uart1_rx_fifo_buf[UART1_RX_FIFO_DEEP * UART1_RX_FIFO_SINGLE_BUFF_SIZE] = {0};

rx_fifo_manage_t uart1_rx_fifo_manage[UART1_RX_FIFO_DEEP] = {0};

/*----------------------------------------------内部函数声明------------------------------------------------------------*/
// 串口1

void uart1_tx_port_cb(uint8_t *udata, uint16_t len);
void uart1_fifo_rx_cb(uint8_t *rdata, uint16_t len);
void uart1_fifo_rx_stop_cb(void);
void uart1_fifo_rx_start_cb(uint8_t *rx_data, uint16_t len);

/*---------------------------------------------------------------------------------------------------------*/


/**
  * @brief  Reception Event Callback (Rx event notification called after use of advanced reception service).
  * @param  huart UART handle
  * @param  Size  Number of data available in application reception buffer (indicates a position in
  *               reception buffer until which, data are available)
  * @retval None
  */
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	if(USART1 == huart->Instance)
	{
		uart_obj_dma_rx_idle_finish(&uart_obj1,Size);
	}	
}



/*------------------------------------------------------------串口框架部分---------------------------------------------------------*/

// printf 重定向到串口 , 串口1作为printf

#include <stdio.h>

int fputc(int ch, FILE *stream)
{
	HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, HAL_MAX_DELAY);
}

/*---------------------------------------------------------------------------------------------------------------------*/

void bsp_uart_obj_init(void)
{
	// 串口1外设初始化
	// HAL已经完成
	
	/*--------------------------------------串口1-对象初始化---------------------------------*/
	// 串口1对象初始化
	uart_obj_sta_t res = uart_obj_ok;
	uart_init_t uart_init = {0};

	// 串口模式选择
//	uart_init.uart_mode = FW_UART_TX_RX; // 串口收发完整模式

	// 发送相关
	uart_init.tx_buf_addr = uart1_tx_buf;
	uart_init.tx_buf_size = sizeof(uart1_tx_buf);
	uart_init.uart_tx_port_cb = uart1_tx_port_cb;

	// 接收相关
	uart_init.rx_time_out_set = 5;										// 接收超时时间，单位ms
	uart_init.rx_fifo_buf_base_addr = uart1_rx_fifo_buf;				// 接收缓冲区基地址
	uart_init.rx_fifo_buf_size_single = UART1_RX_FIFO_SINGLE_BUFF_SIZE; // 单个缓冲区大小
	uart_init.rx_fifo_buf_size_total = sizeof(uart1_rx_fifo_buf);		// 接收缓冲区总大小

	uart_init.rx_fifo_deep = UART1_RX_FIFO_DEEP;	 // 接收FIFO深度
	uart_init.rx_fifo_manage = uart1_rx_fifo_manage; // 接收fifo管理结构体

	uart_init.uart_fifo_rx_cb = uart1_fifo_rx_cb; // 接收FIFO数据回调函数
	
	uart_init.uart_fifo_rx_start_cb = uart1_fifo_rx_start_cb; // 串口接收FIFO启动回调函数

	uart_init.uart_fifo_rx_stop_cb = uart1_fifo_rx_stop_cb;	  // 串口接收FIFO停止回调函数
	
	res = uart_obj_dma_init(&uart_obj1, &uart_init);

	if (res != uart_obj_ok)
	{
		while (1)
		{
		};
	}

	uart_obj_printf(&uart_obj1, "[ msg ]: test uart printf. \r\n");
//	uart_obj_tx_str_data(&uart_obj1,"[ msg ]: test uart str.\r\n");
//	uart_obj_tx_bin_data(&uart_obj1,"[ msg ]: test uart bin.\r\n",26);

//	// 如果只是字符串数据 使用字符串发送省内存
//	uart_obj_tx_str_data(&uart_obj1, "hello cw32f030. \r\n");
//	uart_obj_tx_str_data(&uart_obj1, "this demo test high speed uart obj framework. \r\n");

//	// 测试变量转字符串 , 其意义主要在于不使用可变形参，减少代码空间占用
//	char str_buf[32] = {0};
//	uart_obj_tx_str_data(&uart_obj1, "\r\ntest float to hex: ");
//	uart_obj_tx_str_data(&uart_obj1, float_to_str_hex(123.4567, str_buf, sizeof(str_buf)));
//	uart_obj_tx_str_data(&uart_obj1, "\r\ntest uint32 to hex: ");
//	uart_obj_tx_str_data(&uart_obj1, uint_to_str_hex(123456, str_buf, sizeof(str_buf)));

//	uart_obj_tx_str_data(&uart_obj1, "\r\ntest int32 to dec: ");
//	uart_obj_tx_str_data(&uart_obj1, int_to_str_dec(-123456, str_buf, sizeof(str_buf)));
//	uart_obj_tx_str_data(&uart_obj1, "\r\ntest uint32 to dec: ");
//	uart_obj_tx_str_data(&uart_obj1, uint_to_str_dec(123456, str_buf, sizeof(str_buf)));
//	uart_obj_tx_str_data(&uart_obj1, "\r\ntest float to dec: ");
//	uart_obj_tx_str_data(&uart_obj1, float_to_str_dec(123.4567, str_buf, sizeof(str_buf), 4));
}

void uart1_tx_port_cb(uint8_t *udata, uint16_t len)
{
	HAL_UART_Transmit(&huart1,udata,len,100);
}

void uart1_fifo_rx_cb(uint8_t *rdata, uint16_t len)
{
	// 将接收到的数据发出
	uart_obj_tx_bin_data(&uart_obj1, rdata, len);
}

void uart1_fifo_rx_start_cb(uint8_t *rx_data, uint16_t len)
{
	HAL_UARTEx_ReceiveToIdle_DMA(&huart1,rx_data,len);	

}

void uart1_fifo_rx_stop_cb(void)
{
	HAL_UART_DMAStop(&huart1);
}

void task_uart1_rx_fifo_read(void)
{
	uart_obj_rx_fifo_read(&uart_obj1);
}
